#include "STStack.h"
#include <assert.h>
#include <stdlib.h>


void STInit(stack* st)
{
	assert(st);

	st->a = NULL;
	st->size = st->capacity = 0;
}
void STpush(stack* st, STTypeData x)
{
	assert(st);
	if (st->size == st->capacity)
	{
		int newCapacity = st->capacity == 0 ? 5 : st->capacity * 2;
		STTypeData* tmp = (STTypeData*)realloc(st->a, newCapacity * sizeof(STTypeData));
		if (tmp != NULL)
		{
			st->a = tmp;
			st->capacity = newCapacity;
		}
		else
		{
			perror("realloc fail");
		}
	}
	st->a[st->size++] = x;
}
void STpop(stack* st)
{
	assert(st);
	assert(st->size > 0);

	st->size--;
}
STTypeData STtop(stack* st)
{
	assert(st);
	assert(st->size > 0);

	return st->a[st->size - 1];
}
int STsize(stack* st)
{
	assert(st);

	return st->size;
}
bool STempty(stack* st)
{
	assert(st);

	return st->size == 0;
}
STDestroy(stack* st)
{
	assert(st);
	free(st->a);
	st->size = st->capacity = 0;
}